package com.finance.system.user.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.NamedInheritableThreadLocal;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.ThreadPoolExecutor;

@RestController
public class ThreadLocalTestController {
    private static ThreadLocal inheritableThreadLocal = new NamedInheritableThreadLocal(
            "testLocal");

    @Autowired
    private ThreadPoolExecutor threadPoolExecutor;

    @RequestMapping("threadTest")
    public void test(){

        System.out.println("=============================================");

        inheritableThreadLocal.set(Thread.currentThread().getName() + "a");
        Object o = inheritableThreadLocal.get();
        System.out.println(o);

        System.out.println("=============================================");
        threadPoolExecutor.execute(new Runnable() {
            @Override
            public void run() {
                System.out.println(Thread.currentThread().getName());
                System.out.println(inheritableThreadLocal.get());
                System.out.println("======================= over ======================");

            }
        });
    }

}
